package com.yanqu.road.server.cmd.activity.peakstronghold;

import com.yanqu.road.entity.activity.peakstronghold.config.PeakStrongHoldConfig;
import com.yanqu.road.entity.activity.peakstronghold.config.ePeakStrongholdStageType;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.activity.PeakStrongholdWarProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.peakstronghold.PeakStrongholdModule;
import com.yanqu.road.server.manger.activity.ActivityMgr;
import com.yanqu.road.server.manger.activity.peakstronghold.PeakStrongholdActivityMgr;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.GameProtocol;

@Cmd(code = GameProtocol.S_PEAK_STRONGHOLD_ELIMINATION_ATTACK, desc = "挑战联盟")
public class PeakStrongholdChallengeUnionCmd extends AbstractCommand {

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {

        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        PeakStrongHoldConfig config = PeakStrongholdActivityMgr.getConfig();
        if (config == null || !ActivityMgr.activityInShowTime(config.getActivityInfo())) {
            ret = GameErrorCode.E_ACTIVITY_NO_FOUND;
            return;
        }

        if (!PeakStrongholdActivityMgr.canUnionInBattle(player.getUserInfo().getUnionUid())) {
            ret = GameErrorCode.E_PEAK_HOLD_NOT_IN_ELIMINATE;
            return;
        }
        if (config.getStageType() != ePeakStrongholdStageType.InBattle) {
            ret = GameErrorCode.E_PEAK_HOLD_ELIMINATE_NOT_BEGIN;
            return;
        }
        PeakStrongholdWarProto.AttackPeakEliminatePatronsReqMsg reqMsg = PeakStrongholdWarProto.AttackPeakEliminatePatronsReqMsg.parseFrom(packet.getBytes());
        ret = player.getModule(PeakStrongholdModule.class).attackUnionHold(reqMsg.getTargetUnionId(), reqMsg.getPatronsId());
    }

    @Override
    public void sendData(GamePlayer player) {
        if (ret != 0) {
            PeakStrongholdWarProto.AttackPeakEliminatePatronsRespMsg.Builder resp = PeakStrongholdWarProto.AttackPeakEliminatePatronsRespMsg.newBuilder();
            resp.setRet(ret);
            player.sendPacket(ClientProtocol.U_PEAK_STRONGHOLD_ELIMINATION_ATTACK, resp);
        }
    }
}
